home *** CD-ROM | disk | FTP | other *** search
- /*
- Sample code for parsing QuickDraw GX "QuickDraw PICT" shapes,
- such as those which are rolled into print files when non-GX
- applications print.
-
- 8/30/94 - dmh - Universalized.
-
- Copyright © Apple Computer, 1994
- */
-
- #include <Menus.h>
- #include <Memory.h>
- #include <String.h>
- #include <ToolUtils.h>
- #include <Resources.h>
- #include <Script.h>
- #include <Errors.h>
- #include <Font Routines.h>
- #include <Aliases.h>
- #include <QuickDraw.h>
- #include <Graphics Types.h>
- #include <Graphics Routines.h>
- #include <Graphics Errors.h>
- #include <Math Routines.h>
- #include <Graphics Toolbox.h>
- #include <GXExceptions.h>
-
- // GrafPort wrapper for doing the translation so bottleneck can get to necessary information.
-
- #define kQDBufferSize 1024
-
- typedef struct {
-
- CGrafPort thePort; // The graf port
- short refnum; // file refnum.
- unsigned char buffer[kQDBufferSize]; // input buffer.
- long bufferPosition; // buffer position.
- long bufferSize; // Size of buffer.
- OSErr status; // Place to stick error.
-
- } TQDTranslationGrafPort;
-
-
- OSErr QGXStreamReadBuffer(TQDTranslationGrafPort *translationPort)
- {
- OSErr status;
- long count;
-
- count = kQDBufferSize;
-
- status = FSRead(translationPort->refnum, &count, &(translationPort->buffer[0]));
- translationPort->bufferSize = count;
- translationPort->bufferPosition = 0;
-
- failed_read:
- if (status == eofErr) // end of file is okay.
- status = noErr;
-
- ncheck(status);
- return(status);
-
- }//QGXStreamReadBuffer
-
-
- /************
- BottleNeck for spooling a PICT from disk
- **************/
- pascal void QGXGetPicProc(unsigned char *data, short count)
- {
- OSErr status = noErr;
- long remaining;
- long copy;
- Ptr dataPtr;
- TQDTranslationGrafPort *translationPort;
-
- GetPort(&(GrafPtr)translationPort);
-
- remaining = count;
- dataPtr = data;
- while (remaining > 0) {
-
- if ( (translationPort->bufferSize - translationPort->bufferPosition) >= remaining) {
-
- /** There is enough in the buffer to finish **/
-
- BlockMove(&(translationPort->buffer[translationPort->bufferPosition]), dataPtr, remaining);
- translationPort->bufferPosition += remaining;
- remaining = 0;
-
- } else {
-
- /** Copy the remainder of the buffer, read a new buffer **/
-
- copy = translationPort->bufferSize - translationPort->bufferPosition;
- BlockMove(&(translationPort->buffer[translationPort->bufferPosition]), dataPtr, copy);
- remaining -= copy;
- dataPtr += copy;
-
- status = QGXStreamReadBuffer(translationPort);
- nrequire(status, failed_read);
-
- }//end if
-
- }//end while
-
- failed_read:
-
- translationPort->status = status;
-
- /** Maybe this will make QuickDraw stop drawing the picture **/
-
- if (status != noErr)
- memset(data, 0xFF, (long)count);
-
-
- }//QGXGetPicProc
-
-
- /*****************************************************
-
- GXTranslateQuickDrawPict:
-
- Routine is called by a client to get individual
- GX shapes from the QD pict. The shapes are handed
- to the client using the callback and the refcon in
- the same manner as installing the translator on a
- grafPort.
-
- This routine makes it easy for any client to deal
- with QD shapes.
-
- source: The source QD shape.
-
- ******************************************************/
- OSErr QGXTranslateQuickDrawPict(gxShape source, gxShapeSpoolUPP userFunction, long refCon)
- {
- OSErr status, saveStatus;
- GrafPtr curPort;
- TQDTranslationGrafPort *translationPort;
- gxTag geometryTag;
- gxQuickDrawPict *shapeGeometry;
- FSSpec fileSpec;
- AliasHandle hAlias;
- short fileCount = 1;
- short refnum;
- CQDProcs spoolProcs;
- PicHandle pictToDraw;
- Rect destRect;
- gxRectangle theRectangle;
-
- require_action((QGXGetShapeType(source) == gxQuickDrawPictTag), failed_shapeType, status = -9999;);
-
- /** Make a quickdraw version of the rectangle for the destRect **/
-
- GXGetRectangle(source, &theRectangle);
- destRect.top = theRectangle.top >> 16;
- destRect.bottom = theRectangle.bottom >> 16;
- destRect.left = theRectangle.left >> 16;
- destRect.right = theRectangle.right >> 16;
-
- /******* Get the QuickDraw pict information from the shape ********/
-
- GXGetShapeTags(source, gxQuickDrawPictTag, 1, 1, &geometryTag);
- GXLockTag(geometryTag);
- shapeGeometry = GXGetTagStructure(geometryTag, nil);
-
- nrequire(status = GXGetGraphicsError(nil), failed_getAndLockTag);
-
- /* Make an alias handle from the data in the tag */
-
- hAlias = (AliasHandle) TempNewHandle(shapeGeometry->alias.aliasRecordSize, &status);
- nrequire(status, failed_newHandle);
-
- BlockMove((Ptr)&(shapeGeometry->alias.aliasRecord[0]), *hAlias, shapeGeometry->alias.aliasRecordSize);
-
- /* Get the file spec from the alias handle */
- {
- Boolean needsUpdate = false;
- status = MatchAlias( nil, kARMSearch + kARMNoUI + kARMMountVol,
- hAlias, &fileCount, &fileSpec, &needsUpdate, nil, nil);
- }
- nrequire(status, failed_Match);
-
- /* Now open the file */
-
- status = FSpOpenDF(&fileSpec, fsRdPerm, &refnum);
- nrequire(status, failed_open);
-
- /* Set the file position */
-
- status = SetFPos(refnum, fsFromStart, shapeGeometry->alias.fileOffset);
- nrequire(status, failed_setPos);
-
- /****** Set up a port for the translator with our buffer in it ******/
-
- translationPort = (TQDTranslationGrafPort *) NewPtrSys(sizeof(TQDTranslationGrafPort));
- nrequire((status = MemError()), failed_NewPtr);
-
- translationPort->bufferPosition = 0;
- translationPort->bufferSize = 0;
- translationPort->refnum = refnum;
- GetPort(&curPort);
- OpenCPort((CGrafPtr)translationPort);
- SetPort((GrafPtr)translationPort);
- SetStdCProcs(&spoolProcs);
- spoolProcs.getPicProc = NewQDGetPicProc(QGXGetPicProc);
- translationPort->thePort.grafProcs = &spoolProcs;
-
- /* Draw the data through the translator */
-
- pictToDraw = (PicHandle) TempNewHandle(sizeof(Picture), &status);
- nrequire(status, failed_newPicHandle);
- (*pictToDraw)->picFrame = shapeGeometry->srcRect;
-
- GXInstallQDTranslator((GrafPtr)translationPort, shapeGeometry->options,
- &shapeGeometry->srcRect,
- &destRect,
- shapeGeometry->styleStretch,
- userFunction,
- (void*)refCon);
-
- DrawPicture(pictToDraw, &shapeGeometry->srcRect);
-
- (void)GXRemoveQDTranslator((GrafPtr)translationPort, nil);
-
- DisposeHandle((Handle)pictToDraw);
-
- failed_newPicHandle:
-
- SetPort(curPort);
-
- CloseCPort((CGrafPtr)translationPort);
- DisposePtr((Ptr)translationPort);
-
- failed_NewPtr:
- failed_setPos:
-
- saveStatus = FSClose(refnum);
- ncheck(saveStatus);
- if (status == noErr)
- status = saveStatus;
-
- failed_open:
- failed_Match:
-
- DisposeHandle((Handle)hAlias);
-
- failed_newHandle:
-
- GXUnlockTag(geometryTag);
-
- failed_getAndLockTag:
- failed_shapeType:
-
- return(status);
-
- }//QGXTranslateQuickDrawPict
-
-